package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;

/**
 *
 * @author dysow
 */
public class GestionTransmissionMenu extends List implements CommandListener

{

    private Display display;
    private Displayable previousScreen;
    private MIDlet midlet;
    private ProgressForm progressForm = null;
    private MainMenu mainMenu = null;
    private ConfigurationServeurForm configurationServeurForm = null;
    private String strReceptionAbonne = "Reception des Abonnés";
    private String strModificationParametre = "Modification des paramètres";
    private Command cmdBack;
    private Command cmdQuit;
    private Command cmdOk;
    private static int PROBLEME_RECEPTION = 1;

    public GestionTransmissionMenu(Display display, Displayable previous, MIDlet midlet)
    {
	super("Gestion des Transmissions", List.IMPLICIT);

	this.display = display;
	this.previousScreen = previous;
	this.midlet = midlet;

	this.initialiseComponents();
	this.initialiseCommands();
	this.addCommands();

	this.setCommandListener(this);
	this.setSelectCommand(this.cmdOk);
    }

    private void initialiseComponents()
    {
	this.append(this.strReceptionAbonne, null);
	this.append(this.strModificationParametre, null);
    }

    private void initialiseCommands()
    {
	this.cmdBack = new Command("Back", Command.BACK, 1);
	this.cmdQuit = new Command("Quit", Command.EXIT, 2);
	this.cmdOk = new Command("Ok", Command.OK, 3);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdBack);
	this.addCommand(this.cmdQuit);
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdBack))
	{
	    this.backAction();
	}
	else if (c.equals(this.cmdQuit))
	{
	    this.quitAction();
	}
	else if (c.equals(this.cmdOk))
	{
	    this.okAction();
	}
    }

    private void backAction()
    {
	this.display.setCurrent(this.previousScreen);
    }

    private void quitAction()
    {
	this.midlet.notifyDestroyed();
    }

    private void okAction()
    {
	this.choiceGroupAction();
    }

    public void choiceGroupAction()
    {
	if (this.receptionAbonne())
	{
	    this.receptionAbonneAction();
	}
	else if (this.modifierParametre())
	{
	    this.modificationParametreAction();
	}
    }

    public boolean receptionAbonne()
    {
	return this.getString(this.getSelectedIndex()).equals(this.strReceptionAbonne);
    }

    public boolean modifierParametre()
    {
	return this.getString(this.getSelectedIndex()).equals(this.strModificationParametre);
    }

    private void receptionAbonneAction()
    {
	try
	{
	    this.progressForm = new ProgressForm("Reception Abonnés",
		    "L'application est en train de télécharger les données abonnés. Veuillez patienter...",
		    this, this.display);
	    this.display.setCurrent(this.progressForm);
            System.out.println("Données en reception : ");
	    IBusinessServices businessServices = BusinessServicesImpl.getInstance();
	  //  businessServices.receptionDonnes(this);
	}
	catch (Exception ex)
	{
	    Alert alert = new Alert("Erreur Connexion",
		    "Une erreur s'est produite pendant la transmission des données. Veuillez re-essayez.", null, AlertType.ERROR);
	    this.display.setCurrent(alert);
	}
    }

    private void modificationParametreAction()
    {
	this.display.setCurrent(this.getConfigurationServeurForm());
    }

    public void displayAlert(String errorDescription)
    {

//	OkAlert alert = new OkAlert(this, GestionTransmissionMenu.PROBLEME_RECEPTION);
//	alert.setDetails("Erreur", errorDescription, null, AlertType.ERROR);
//	this.display.setCurrent(new ErrorsForm(errorDescription));

	InformationForm informationForm = new InformationForm(this.display, this);
	informationForm.setMessage(errorDescription);
	this.display.setCurrent(informationForm);
    }

    public void receptionSucces(int nombreAbonnes)
    {
	InformationForm informationForm = new InformationForm(this.display, this.getMainMenu());
	informationForm.setMessage(nombreAbonnes + " abonnés ont été receptionés.");
	this.display.setCurrent(informationForm);
    }

    public void setThread(Thread thread)
    {
	this.progressForm.setThread(thread);
    }

    public void receptionAbonneSucces(Vector vector)
    {
	try
	{
	    IBusinessServices businessServices = BusinessServicesImpl.getInstance();
	    businessServices.reinitialiserAbonneList(vector);
            businessServices.updateIdentifiantsStore(vector);
	}
	catch (RecordStoreException ex)
	{
	    Alert alert = new Alert("Erreur entrée données abonnés", "Une erreur s'est produite pendant l'entrée des données abonnés", null,
		    AlertType.ERROR);
	    this.display.setCurrent(alert);

	    ex.printStackTrace();
	}
	catch (IOException ex)
	{
	    ex.printStackTrace();
	}
    }

    public void receptionConvocationSucces(Vector vector)
    {
	try
	{
	    IBusinessServices businessServices = BusinessServicesImpl.getInstance();
	    businessServices.reinitialiserConvocationList(vector);
	}
	catch (RecordStoreException ex)
	{
	    Alert alert = new Alert("Erreur entrée données convocations",
		    "Une erreur s'est produite pendant l'entrée des données convocations", null,
		    AlertType.ERROR);
	    this.display.setCurrent(alert);

	    ex.printStackTrace();
	}
	catch (IOException ex)
	{
	    ex.printStackTrace();
	}
    }

    public void envoieSucces()
    {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    private MainMenu getMainMenu()
    {
	if (this.mainMenu == null)
	{
	    this.mainMenu = new MainMenu(this.midlet);
	}

	return this.mainMenu;
    }

    private ConfigurationServeurForm getConfigurationServeurForm()
    {
	if (this.configurationServeurForm == null)
	{
	    this.configurationServeurForm = new ConfigurationServeurForm(this.display, this);
	}

	return this.configurationServeurForm;
    }
}
